//
//  LYLivePlayingCell.m
//  LYLiveApp
//
//  Created by QD on 2018/6/25.
//  Copyright © 2018年 览益信息科技. All rights reserved.
//

#import "LYLivePlayingCell.h"
#import "LYIntroduceModel.h"
#import "LYLiveDescCell.h"

@interface LYLivePlayingCell ()
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *descLabelHeight;
@property (weak, nonatomic) IBOutlet UIButton *watchLiveBtn;
@property (weak, nonatomic) IBOutlet UIView *liveStateView;
@property (weak, nonatomic) IBOutlet UIImageView *liveStateImage;

@end

@implementation LYLivePlayingCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    NSMutableArray *imgs = [NSMutableArray array];
    for (int i=1; i<4; i++) {
        NSString *imgName = [NSString stringWithFormat:@"video_living_red%d",i];
        UIImage *img = [UIImage imageNamed:imgName];
        [imgs addObject:img];
    }
    self.liveStateImage.animationImages = imgs;
    self.liveStateImage.animationRepeatCount = 0;
    self.liveStateImage.animationDuration = 0.6;
    [self.liveStateImage startAnimating];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"LYLivePlayingCell";
    LYLivePlayingCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"LYLivePlayingCell" owner:self options:nil] lastObject];
    }
    return cell;
}

- (IBAction)videoLiveAction:(id)sender {
    
    !self.videoLiveBlock?:self.videoLiveBlock(self.roomListModel);
}

- (void)setRoomListModel:(LYRoomListModel *)roomListModel {
    
    _roomListModel = roomListModel;
    self.timeLabel.text = roomListModel.startTime;
    if (![LYUtil isNilOrEmpty:roomListModel.actTeacher]) {
        self.nameLabel.text = [NSString stringWithFormat:@"%@-%@",roomListModel.actName,roomListModel.actTeacher];
    } else {
        self.nameLabel.text = [NSString stringWithFormat:@"%@",roomListModel.actName];
    }
    
    if (roomListModel.isPlayLiving) {
        self.liveStateView.hidden = NO;
        self.watchLiveBtn.hidden = YES;
    } else {
        self.liveStateView.hidden = YES;
        self.watchLiveBtn.hidden = NO;
    }
    
    CGFloat labelHeight = [LYUtil sizeWithText:roomListModel.foreshow font:self.descLabel.font withWidth:SCREEN_WIDTH - 65].height;
    if (labelHeight < 20) {
        labelHeight = 16;
    }
    self.descLabelHeight.constant = labelHeight;
    self.descLabel.text = roomListModel.foreshow;
    
    WS(weakSelf);
    CGFloat originY = 0;
    for (int i = 0; i < roomListModel.teachers.count; i ++) {
        
        LYLiveDescCell *cell = [LYLiveDescCell viewWithXib];
        cell.origin = CGPointMake(0, originY);
        cell.width = self.containerView.width;
        cell.teacherModel = roomListModel.teachers[i];
        cell.joinTeamBlock = ^{
            !weakSelf.joinTeamBlock?:weakSelf.joinTeamBlock();
        };
        [self.containerView addSubview:cell];
        originY += (cell.teacherModel.viewHeight + 10);
      
        self.height = originY + labelHeight + 70;
    }
}

@end
